package org.minuframework.http;

import java.util.Enumeration;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.minuframework.lang.StringUtils;

@Deprecated
public class HttpSessionBuilder{
    
    private HttpSession         httpSession;
    private ServletContext      servletContext;
    private Enumeration<String> attributeNames;
    private long                creationTime;
    private String              id;
    private long                lastAccessedTime;
    private int                 maxInactiveInterval;
    private boolean             isNew;
    
    public HttpSessionBuilder(HttpServletRequest request){
        this.httpSession = request.getSession();
        this.servletContext = httpSession.getServletContext();
        this.attributeNames = httpSession.getAttributeNames();
        this.creationTime = httpSession.getCreationTime();
        this.id = httpSession.getId();
        this.lastAccessedTime = httpSession.getLastAccessedTime();
        this.maxInactiveInterval = httpSession.getMaxInactiveInterval();
        this.isNew = httpSession.isNew();
    }
    
    public HttpSession getHttpSession(){
        return httpSession;
    }
    
    public long getCreationTime(){
        if (creationTime <= 0) {
            this.creationTime = httpSession.getCreationTime();
        }
        return creationTime;
    }
    
    public String getId(){
        if (StringUtils.isEmpty(id)) {
            this.id = httpSession.getId();
        }
        return id;
    }
    
    public long getLastAccessedTime(){
        if (lastAccessedTime <= 0) {
            this.lastAccessedTime = httpSession.getLastAccessedTime();
        }
        return lastAccessedTime;
    }
    
    public ServletContext getServletContext(){
        if (servletContext == null) {
            this.servletContext = httpSession.getServletContext();
        }
        return servletContext;
    }
    
    public void setMaxInactiveInterval(int interval){
        httpSession.setMaxInactiveInterval(interval);
    }
    
    public int getMaxInactiveInterval(){
        if (maxInactiveInterval <= 0) {
            this.maxInactiveInterval = httpSession.getMaxInactiveInterval();
        }
        return maxInactiveInterval;
    }
    
    public Object getAttribute(String name){
        return httpSession.getAttribute(name);
    }
    
    public Enumeration<String> getAttributeNames(){
        if (attributeNames == null) {
            this.attributeNames = httpSession.getAttributeNames();
        }
        return attributeNames;
    }
    
    public HttpSession setAttribute(String name, Object value){
        httpSession.setAttribute(name, value);
        return httpSession;
    }
    
    public HttpSession removeAttribute(String name){
        httpSession.removeAttribute(name);
        return httpSession;
    }
    
    public HttpSession invalidate(){
        httpSession.invalidate();
        return httpSession;
    }
    
    public boolean isNew(){
        if (isNew == false) {
            this.isNew = httpSession.isNew();
        }
        return isNew;
    }
    
}
